package com.itic.appbase.applications.openoffice;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.itic.appbase.framework.utils.CommandHelper;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.appbase.framework.utils.configuration.PropertyHelper;

/**
 * openoffice服务类
 * <p>Company: itic</p>
 * @author: bianyuwu
 * @date: 2015年5月8日 上午9:19:52
 * @version: V1.0
 */
public class OpenOfficeServer {

    private static final Logger log = LoggerFactory.getLogger(PdfToSwf.class);
    private String openOfficePath;// openOffice安装路径
    private static OpenOfficeConnection connection = null;
            
    /**
     * 设置openoffice路径.
     * @param openOfficePath openoffice安装路径program目录前
     * @throws
     */
    public OpenOfficeServer(String openOfficePath) {
        this.openOfficePath = openOfficePath;
    }
    
    /**
     * 获得openoffice服务连接.
     * @return connection
     */
    public static synchronized OpenOfficeConnection getConnection() {
        if (connection == null) {
            int port = PropertyHelper.getInt("openoffice.port");
            connection = new SocketOpenOfficeConnection("127.0.0.1", port);
        }
        return connection;
    }
    
    /**
     * 启动openoffice服务.
     * @return result
     */
    public boolean start() {
        if (StringHelper.isBlank(openOfficePath)) {
            log.error("[OpenOfficeServer | start]：openOfficePath 参数为空");
            return false;
        }
        String command = openOfficePath
                + "program/soffice -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\" -nofirststartwizard";
        log.debug("[OpenOfficeServer | start]： 即将开启openOffice服务的命令是：" + command);
        return CommandHelper.runCommand(command) == 0;
    }
    
}